#include "..\config.h"
#include "ssd1283a.H"

void myDelay (unsigned int ulTime)
{
    unsigned int  i;
    
    i = 0;
    while (ulTime--) {
        for (i = 0; i < 5; i++);
    }
}

void LCD_Init()
{
	int i;
	
	RSTInit();
	CSInit();
	DATAInit();
	CLKInit();
	CDInit();
	
	Reset();
	Write_WDR(POWER_CONTROL_1,0x2f8e);
	Write_WDR(POWER_CONTROL_2,0x000c);
	Write_WDR(DISPLAY_CONTUROL,0x0021);
	Write_WDR(0x28,0x0006);
	Write_WDR(0x28,0x0005); 
	Write_WDR(0x27,0x057f); 
	Write_WDR(0x29,0x89a1);
	Write_WDR(START_OSCILLATION, 0x0001); 

    myDelay(100);				

	Write_WDR(0x29,0x80b0); 		
    myDelay(30);
	Write_WDR(0x29,0xfffe);		
	Write_WDR(DISPLAY_CONTUROL,0x0023);
    myDelay(30);
	Write_WDR(DISPLAY_CONTUROL,0x0033);		
	Write_WDR(DEVICE_OUTPUT_CONTROL,0x2183);		
	Write_WDR(0x2f,0xffff);		
	Write_WDR(0x2c,0x8000);
	Write_WDR(ENTRY_MODE,0x6830); 		
	Write_WDR(0x27,0x0570);
	Write_WDR(LCD_DRIVING_AC_CONTROL, 0x0300); 		
	Write_WDR(FRAME_CYCLE_CONTROL, 0x580c);
	Write_WDR(POWER_CONTROL_3, 0x0609);
	Write_WDR(POWER_CONTROL_4, 0x3100);	
	Write_WDR(0x2a,0x1dd0);
	Write_WDR(0x2b,0x0a90);	
	Write_WDR(0x2d,0x310f);
    myDelay(100);
 			
	Write_WDR(POWER_CONTROL_5, 0x00bf);	
    myDelay(1);
	Write_WDR(POWER_CONTROL_5, 0x0000);
    myDelay(100);
	Write_WDR(VERTICAL_WINDOW_ADDRESS, 0x8300);		
	Write_WDR(HORIZONTAL_WINDOW_ADDRESS, 0x8300);
	Write_WDR(RAM_ADDRESS_SET, 0x0000);			
	Write_IR(0x22);
	for (i = 0; i < 17424; i++)
 	{
		Write_Byte(0xbe);
		Write_Byte(0x5f);
	}
}

void Reset(void)
{
    RSTON();
	myDelay(1);
	RSTOFF();
	myDelay(1);
    RSTON();
} 


void Write_Byte(unsigned char data)
{
	unsigned char i;
	
	CDON();
	CSOFF();
	
    for(i = 0; i < 8; i++)
	{
		CLKOFF();
		
		if((data & 0x80) != 0)
			DATAON();
		else DATAOFF();
		
		data = data << 1;
		
		CLKON();
		CLKOFF();
	}
 	CSON();
}

void Write_Word(unsigned int dat)
{
	unsigned char dataH, dataL;
	
	dataH = dat/256;
	dataL = dat&0xff;
	Write_Byte(dataH);
	Write_Byte(dataL);
}

void Write_IR(unsigned char reg)
{
	unsigned char i;

	CDOFF();
    CSOFF();

	for(i=0;i<8;i++)
	{
		CLKOFF();
		
		if((reg & 0x80) != 0)
			DATAON();
		else DATAOFF();
		
		reg = reg << 1;
		
		CLKON();
		CLKOFF();
     }
 	CSON();	
}

void Write_WDR(unsigned char reg,unsigned int dat)
{
	Write_IR(reg);
	Write_Word(dat);
}
